package itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import itheima.reggie.common.CustomException;
import itheima.reggie.common.GlobalExceptionHandler;
import itheima.reggie.dto.SetmealDto;
import itheima.reggie.entity.Setmeal;
import itheima.reggie.entity.SetmealDish;
import itheima.reggie.mapper.SetmealMapper;
import itheima.reggie.service.SetmealDishService;
import itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
@Transactional
@Service("setmealServiceImpl")
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 保存套餐与菜品
     * @param dto
     */
    @Transactional
    @Override
    public void saveSetmealWithDish(SetmealDto dto) {
        //保存基本信息
        this.save(dto);
        //保存套餐和菜品关联信息
        //设置菜品id
        List<SetmealDish> list = dto.getSetmealDishes().stream().map((item -> {
            item.setSetmealId(dto.getId());
            return item;
        })).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    @Override
    public SetmealDto queryById(Long id) {
        Setmeal setmeal = this.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        SetmealDto dto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,dto);
        dto.setSetmealDishes(list);
        return dto;
    }

    /**
     * 修改信息
     * @param dto
     */
    @Override
    public void editSetmeal(SetmealDto dto) {
        this.updateById(dto);
        //清理久的套餐信息
        Long setmealId = dto.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);
        //保存新的套餐菜品信息
        List<SetmealDish> list = dto.getSetmealDishes().stream().map((item -> {
            item.setSetmealId(dto.getId());
            return item;
        })).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void deleteSetmealWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("无法删除售卖中的套餐！！");
        }
        //如果不能删除，抛出一个业务异常
        this.removeByIds(ids);
        //可以删除，先删除表中数据，再删除关联数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }
}
